/*
 * PlaceTowers - tower placement phase of play
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#if flash9
import flash.events.TimerEvent;
import flash.Lib;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.BitmapData;
import flash3f.InputHandler;
import flash3f.Images;
import flash.geom.Point;
#else neko
import neko3f.InputHandler;
import lib3f.Point;
#end
import lib3f.IntPoint;
import lib3f.Dist;

//// base rules for tower placement
// 1. Distance/adjacency - 2 units of Manhattan distance
// 2. Space is free; or space has non-upgraded tower, to upgrade
// 3. Tower must be provided with a target inside the map area

//// things the interface should do
// 1. Tower/Wall modes
// 2. Place tower with mouse, then select target
// 3. Undo
// 4. Drag-place walls

class PlaceTowers
{
	static public var checkforspace : Bool;
	static public var towerstoplace : Int;
	static public var mode : String;
	static public var lastx : Int;
	static public var lasty : Int;
	static public var lastshooter : Shooter;
	static public var targetstart : Point;
	static public var upgradetarget : GameTile;

	static public var finished : Bool;

	#if flash9
	static public var mypop : PopTart;
	static public var okareaarray : Array<IsoBitmap>;
	//static public var targetsprite : Sprite = new Sprite();
	//static public var targetcache : Array<Array<Int>>;
	//static public var targetbg : Bitmap;
	static public var tutorialtickerplace : Int;
	static public var tutorialtickertarget : Int;
	static public var targetline : Shape; // line from tower to target
	static public var targetbox : Shape; // target area shape
	static public var targetcache : Bitmap;
	static public var showcache : Shiner;
	#end

	static public function reportnumtowers() : Int
	{
		return Std.int(FortressManager.calcterritory()/20)+1;
	}

	static public function reportnumwalls() : Int
	{
		return 8;
	}

	static public function start()
	{
		
	}
	
	/*static public function start()
	{
		finished = false;
		Gameplay.screenPlacement();
		InputHandler.clear();
		towerstoplace = Std.int(FortressManager.calcterritory()/20)+1;
		if (FortressManager.nospaceleft() && FortressManager.towerscantbeupgraded())
			towerstoplace = 0;
		#if flash9
		
		okareaarray = new Array();
		
		tutorialtickerplace = 1;
		tutorialtickertarget = 1;
		if (towerstoplace>0)
		{
			if (towerstoplace>1)
				mypop = new PopTart(["PLACE "+Std.string(towerstoplace)+" TOWERS OR UPGRADES"]);
			else
				mypop = new PopTart(["PLACE 1 TOWER OR UPGRADE"]);
			mypop.waitforever = true;
		}
		
		calc_validtower();

		targetcache = new Bitmap();
		targetcache.visible = false;
		targetcache.bitmapData = new BitmapData(680,425,true,0x000000);
		draw_targetcache();
		Lib.current.addChild(targetcache);
		
		targetline = new Shape();
		targetline.visible = false;
		Lib.current.addChild(targetline);

		targetbox = new Shape();
		targetbox.visible = false;
		Lib.current.addChild(targetbox);

		upgradedist = new Shiner(Fonts.verdanaitalic.gen(["Larger Range and Shot Speed"]));
		upgradedist.visible = false;
		upgradedist.x = 680/2 - (upgradedist.width/2);
		upgradedist.y = 425/2 - 425/6 - (upgradedist.height/2);
		Lib.current.addChild(upgradedist);
		
		upgradepower = new Shiner(Fonts.verdanaitalic.gen(["Double Shot Power"]));
		upgradepower.visible = false;
		upgradepower.x = 680/2 - (upgradepower.width/2);
		upgradepower.y = 425/2 - (upgradepower.height/2);
		Lib.current.addChild(upgradepower);

		upgradeshots = new Shiner(Fonts.verdanaitalic.gen(["Fast Fire Rate"]));
		upgradeshots.visible = false;
		upgradeshots.x = 680/2 - (upgradeshots.width/2);
		upgradeshots.y = 425/2 + 425/6 - (upgradeshots.height/2);
		Lib.current.addChild(upgradeshots);

		showcache = new Shiner(Fonts.verdanaitalic.gen(["Show Targets"]));
		showcache.x = 4;
		showcache.y = 425 - 42;
		Lib.current.addChild(showcache);
		
		#end

		checkforspace = true;
		
	}*/	

	static public function test_show_targets() : Void
	{
		#if flash9
		if (showcache.bitmapData.rect.contains
				(InputHandler.cursx-showcache.x,InputHandler.cursy-showcache.y))
			targetcache.visible = true;
		else
			targetcache.visible = false;
		#end
	}
	
	/*static public function up() : Void 
	{
	
		tutorial();
	
		if (mode=="place")
		{
			test_show_targets();
		
			input_place();
		}
		else if (mode=="upgrade")
		{
			input_upgrade();
		}
		else
		{
			draw_currenttarget();
			input_target();
		}
	}*/

	static public function noroom()
	{
		if (checkforspace && FortressManager.nospaceleft() && FortressManager.towerscantbeupgraded())
		{
			#if flash9
			mypop = new PopTart(["NO ROOM FOR TOWERS!"]);
			#end
			towerstoplace = 0;
		}
		checkforspace = false;	
	}
	
	static public function tutorial()
	{
		#if flash9
		if (mode=="place")
		{
			tutorialtickerplace = (tutorialtickerplace+1) % (30*24);
			if (tutorialtickerplace==0)
			{
				if (towerstoplace>1)
					mypop = new PopTart(["PLACE "+Std.string(towerstoplace)+" TOWERS OR UPGRADES"]);
				else
					mypop = new PopTart(["PLACE 1 TOWER OR UPGRADE"]);
			}
			else if (tutorialtickerplace==(30*6))
			{
				mypop = new PopTart(["GREEN AREAS ARE OK TO PLACE"]);				
			}
			else if (tutorialtickerplace==(30*12))
			{
				mypop = new PopTart(["20 SQ. OF TERRITORY = 1 TOWER"]);				
			}
			else if (tutorialtickerplace==(30*18))
			{
				mypop = new PopTart(["YOU GET 2 FREE TOWERS PER ROUND"]);				
			}
		}
		else
		{
			tutorialtickertarget = (tutorialtickertarget+1) % (30*24);
			if (tutorialtickertarget==0)
			{
				mypop = new PopTart(["SELECT TARGET"]);			
			}
			if (tutorialtickertarget==(30*4))
			{
				mypop = new PopTart(["SELECT A 3X3 SQUARE..."]);
			}
			else if (tutorialtickertarget==(30*8))
			{
				mypop = new PopTart(["THE TOWER WILL TRACK CREEPS..."]);		
			}	
			else if (tutorialtickertarget==(30*12))
			{
				mypop = new PopTart(["IN THIS SQUARE"]);		
			}
			else if (tutorialtickertarget==(30*16))
			{
				mypop = new PopTart(["CASTLES HAVE A LARGER RADIUS"]);		
			}
			else if (tutorialtickertarget==(30*20))
			{
				mypop = new PopTart(["UPGRADES IMPROVE TOWER FIREPOWER"]);		
			}
		}
		mypop.waitforever = true;
		#end
	}
	
	/*static public function end()
	{
		#if flash9
		mypop.waitforever = false;
		mypop.waittime = 0;
		Lib.current.removeChild(targetline);
		Lib.current.removeChild(targetbox);
		Lib.current.removeChild(targetcache);
		Lib.current.removeChild(upgradedist);
		Lib.current.removeChild(upgradepower);
		Lib.current.removeChild(upgradeshots);
		Lib.current.removeChild(showcache);
		for (n in okareaarray)
			IsoManager.remove(n);
		#end
		finished = true;
	}*/
	
	static public function setlast()
	{
		lastx = HUD.ctx;
		lasty = HUD.cty;
		targetstart = IsoMetrics.tiletopix_xy(HUD.ctx, HUD.cty);
		lastshooter = Game.cur.map[lastx][lasty].fort.shooter;	
	}
	
	static public function finishplace()
	{
		FortressManager.calcterritory();
		towerstoplace-=1;
		mode = "target";
		
		#if flash9
			SFX.towerplacement = true;
			mypop = new PopTart(["SELECT TARGET"]);
			mypop.waitforever = true;
			targetcache.visible = true;
			for (n in okareaarray)
			{
				IsoManager.remove(n);
			}
			okareaarray = new Array();					
			draw_targetcache();
			calc_validtarget();
			targetline.visible = true;
			targetline.graphics.clear();
			targetbox.visible = true;
			targetbox.graphics.clear();
		#end
		
		checkforspace = true;	
	}

	static public function input_upgrade()
	{
		if (InputHandler.click)
		{
			if (InputHandler.cursy<425/2 - 425/8)
			{
				upgradetarget.fort.upgrade(2);
				finishplace();
			}
			else if (InputHandler.cursy>=425/2 - 425/8 && InputHandler.cursy<=425/2 + 425/8)
			{
				upgradetarget.fort.upgrade(3);
				finishplace();
			}
			else 
			{
				upgradetarget.fort.upgrade(4);
				finishplace();
			}

			#if flash9
			upgradedist.visible = false;
			upgradepower.visible = false;
			upgradeshots.visible = false;
			#end
		}
	}
	
	static public function input_place()
	{	
	
		if (InputHandler.click)
		{

		
			if (Graph.inbounds(HUD.ctx, HUD.cty, Game.cur.map) && Game.cur.map[HUD.ctx][HUD.cty].fort!=null && Game.cur.map[HUD.ctx][HUD.cty].fort.shooter!=null)
			{
				var cur = Game.cur.map[HUD.ctx][HUD.cty];
				if (cur.fort.shooter.weapon==1)
				{
					mode = "upgrade";
					upgradetarget = cur;
					#if flash9
					upgradedist.visible = true;
					upgradepower.visible = true;
					upgradeshots.visible = true;
					#end
					setlast();
				}
			}
			else
			{
		
				FortressManager.test(HUD.ctx,HUD.cty);
			
				if (FortressManager.test(HUD.ctx,HUD.cty) &&
					FortressManager.farfort(HUD.ctx,HUD.cty)		
				)
				{
					FortressManager.addtower(HUD.ctx,HUD.cty);
					for (n in Game.cur.shooterman.shooters) n.retarget();
					setlast();
					finishplace();				
				}
			}
		}				

		
		if (InputHandler.wheeldelta==true || InputHandler.rotleftkey)
		{	

			// select tower

		}
		else if (InputHandler.rotrightkey)
		{

			// select tower
		
		}

			// add mouseover select towers
		
			// add descriptions?
				
	}	
	
	static public function input_target()
	{	
	
		if (InputHandler.click && 
			HUD.ctx>0 && HUD.cty>0 && HUD.ctx<25 && HUD.cty<25 &&
			Dist.IntPoint(new IntPoint(HUD.ctx, HUD.cty), 
			              new IntPoint(lastx, lasty))<lastshooter.dist)
		{

			lastshooter.target = new IntPoint(HUD.ctx, HUD.cty);
			mode = "place";
			calc_validtower();
			
			if (towerstoplace>0 && !(FortressManager.nospaceleft() && FortressManager.towerscantbeupgraded()))
			{
				#if flash9
				if (towerstoplace>1)
					mypop = new PopTart(["PLACE ",towerstoplace," TOWERS OR UPGRADES"]);
				else
					mypop = new PopTart(["PLACE 1 TOWER OR UPGRADE"]);
				mypop.waitforever = true;
				//targetbg.visible = false;
				//targetsprite.visible = false;
				draw_targetcache();
				targetline.visible = false;
				targetbox.visible = false;
				targetcache.visible = false;
				#end
			}
			else
			{
				ComponentManager.remove(PlaceTowers);
			}
			
		}				

		
		if (InputHandler.wheeldelta==true || InputHandler.rotleftkey)
		{	

			// select tower

		}
		else if (InputHandler.rotrightkey)
		{

			// select tower
		
		}

			// add mouseover select towers
		
			// add descriptions?
				
	}	

	static public function calc_validtower()
	{
	
		#if flash9
		for (n in okareaarray)
		{
			IsoManager.remove(n);
		}

		okareaarray = new Array();
		
		var final : Array<IntPoint> = new Array();
		
		for (n in Graph.iter(Game.cur.map))
		{
			if (FortressManager.test(n.x, n.y) && FortressManager.farfort(n.x,n.y))
			final.push(new IntPoint(n.x, n.y));
		}
		
		for (n in final)
		{
			var nb : IsoBitmap = new IsoBitmap(Images.getdata(0xFD00FF));
			var npc : Point = IsoMetrics.tiletopix_xy(n.x, n.y);
			nb.offy = 0;
			nb.offx = 0;
			nb.movep(npc);
			okareaarray.push(nb);
			IsoManager.add(nb);		
		}
		#end
		
	}
	
	static public function calc_validtarget()
	{

		#if flash9

		var boxstart = IsoMetrics.tiletopix_xy(lastx, lasty);
		boxstart.x+=12;
		boxstart.y-=0;

		var cachebox : Shape = new Shape();

		cachebox.graphics.clear();
		cachebox.graphics.lineStyle(2,0xFFFF22,0.75);
		cachebox.graphics.beginFill(0x888888,0);

		var pts : Array<Point> = new Array();

		if (lastshooter.weapon!=2)
		{
			pts.push(IsoMetrics.tilecorner_left(lastx-4, lasty-3));
			pts.push(IsoMetrics.tilecorner_bottom(lastx-4, lasty-3));
			pts.push(IsoMetrics.tilecorner_right(lastx-4, lasty-3));
			pts.push(IsoMetrics.tilecorner_left(lastx-3, lasty-4));
			pts.push(IsoMetrics.tilecorner_bottom(lastx-3, lasty-4));
			pts.push(IsoMetrics.tilecorner_right(lastx-3, lasty-4));

			pts.push(IsoMetrics.tilecorner_top(lastx+3, lasty-4));
			pts.push(IsoMetrics.tilecorner_left(lastx+3, lasty-4));
			pts.push(IsoMetrics.tilecorner_bottom(lastx+3, lasty-4));
			pts.push(IsoMetrics.tilecorner_top(lastx+4, lasty-3));
			pts.push(IsoMetrics.tilecorner_left(lastx+4, lasty-3));
			pts.push(IsoMetrics.tilecorner_bottom(lastx+4, lasty-3));

			pts.push(IsoMetrics.tilecorner_right(lastx+4, lasty+3));
			pts.push(IsoMetrics.tilecorner_top(lastx+4, lasty+3));
			pts.push(IsoMetrics.tilecorner_left(lastx+4, lasty+3));
			pts.push(IsoMetrics.tilecorner_right(lastx+3, lasty+4));
			pts.push(IsoMetrics.tilecorner_top(lastx+3, lasty+4));
			pts.push(IsoMetrics.tilecorner_left(lastx+3, lasty+4));

			pts.push(IsoMetrics.tilecorner_bottom(lastx-3, lasty+4));
			pts.push(IsoMetrics.tilecorner_right(lastx-3, lasty+4));
			pts.push(IsoMetrics.tilecorner_top(lastx-3, lasty+4));
			pts.push(IsoMetrics.tilecorner_bottom(lastx-4, lasty+3));
			pts.push(IsoMetrics.tilecorner_right(lastx-4, lasty+3));
			pts.push(IsoMetrics.tilecorner_top(lastx-4, lasty+3));

			}
		
		else if (lastshooter.weapon==2)
		{
			pts.push(IsoMetrics.tilecorner_left(lastx-6, lasty-4));
			pts.push(IsoMetrics.tilecorner_bottom(lastx-6, lasty-4));
			pts.push(IsoMetrics.tilecorner_right(lastx-6, lasty-4));
			pts.push(IsoMetrics.tilecorner_left(lastx-5, lasty-5));
			pts.push(IsoMetrics.tilecorner_bottom(lastx-5, lasty-5));
			pts.push(IsoMetrics.tilecorner_right(lastx-5, lasty-5));
			pts.push(IsoMetrics.tilecorner_left(lastx-4, lasty-6));
			pts.push(IsoMetrics.tilecorner_bottom(lastx-4, lasty-6));
			pts.push(IsoMetrics.tilecorner_right(lastx-4, lasty-6));

			pts.push(IsoMetrics.tilecorner_top(lastx+4, lasty-6));
			pts.push(IsoMetrics.tilecorner_left(lastx+4, lasty-6));
			pts.push(IsoMetrics.tilecorner_bottom(lastx+4, lasty-6));
			pts.push(IsoMetrics.tilecorner_top(lastx+5, lasty-5));
			pts.push(IsoMetrics.tilecorner_left(lastx+5, lasty-5));
			pts.push(IsoMetrics.tilecorner_bottom(lastx+5, lasty-5));
			pts.push(IsoMetrics.tilecorner_top(lastx+6, lasty-4));
			pts.push(IsoMetrics.tilecorner_left(lastx+6, lasty-4));
			pts.push(IsoMetrics.tilecorner_bottom(lastx+6, lasty-4));

			pts.push(IsoMetrics.tilecorner_right(lastx+6, lasty+4));
			pts.push(IsoMetrics.tilecorner_top(lastx+6, lasty+4));
			pts.push(IsoMetrics.tilecorner_left(lastx+6, lasty+4));
			pts.push(IsoMetrics.tilecorner_right(lastx+5, lasty+5));
			pts.push(IsoMetrics.tilecorner_top(lastx+5, lasty+5));
			pts.push(IsoMetrics.tilecorner_left(lastx+5, lasty+5));
			pts.push(IsoMetrics.tilecorner_right(lastx+4, lasty+6));
			pts.push(IsoMetrics.tilecorner_top(lastx+4, lasty+6));
			pts.push(IsoMetrics.tilecorner_left(lastx+4, lasty+6));

			pts.push(IsoMetrics.tilecorner_bottom(lastx-4, lasty+6));
			pts.push(IsoMetrics.tilecorner_right(lastx-4, lasty+6));
			pts.push(IsoMetrics.tilecorner_top(lastx-4, lasty+6));		
			pts.push(IsoMetrics.tilecorner_bottom(lastx-5, lasty+5));
			pts.push(IsoMetrics.tilecorner_right(lastx-5, lasty+5));
			pts.push(IsoMetrics.tilecorner_top(lastx-5, lasty+5));		
			pts.push(IsoMetrics.tilecorner_bottom(lastx-6, lasty+4));
			pts.push(IsoMetrics.tilecorner_right(lastx-6, lasty+4));
			pts.push(IsoMetrics.tilecorner_top(lastx-6, lasty+4));		
		}
			
		cachebox.graphics.moveTo(pts[0].x, pts[0].y);
		
		for(p in pts)
		{
			cachebox.graphics.lineTo(p.x, p.y);
		}
		
		cachebox.graphics.endFill();

		targetcache.bitmapData.draw(cachebox);
		
		#end
		
		
	
	}

	static public function draw_targetcache()
	{
		// draw the shape and line of each target.
		
		#if flash9

		targetcache.bitmapData = new BitmapData(680,425,true,0x000000);
		
		for (shoot in Game.cur.shooterman.shooters)
		{

			if (shoot.target!=null)
			{
				var boxstart = IsoMetrics.tiletopix_xy(shoot.target.x, shoot.target.y);
				boxstart.x+=12;
				boxstart.y-=22;
				var towerstart = IsoMetrics.tiletopix_xy(shoot.location.x, shoot.location.y);
			
				var distx : Float = boxstart.x-towerstart.x;
				var disty : Float = boxstart.y-towerstart.y;
				
				var tdist : Float = Math.sqrt(distx*distx + disty*disty);
				
				var dirx : Float = (distx/tdist);
				var diry : Float = (disty/tdist);

				var cachebox : Shape = new Shape();
				var cacheline : Shape = new Shape();
				
				cacheline.graphics.clear();

				cacheline.graphics.lineStyle(1,0xFFFFFF,0.75);
	
				cacheline.graphics.beginFill(0xFFFFFF,0.1);
				cacheline.graphics.moveTo(towerstart.x+11+(diry*3),towerstart.y+4-(dirx*3));
				cacheline.graphics.lineTo(towerstart.x+11-(diry*3),towerstart.y+4+(dirx*3));
				cacheline.graphics.lineTo(boxstart.x,boxstart.y+33);
				cacheline.graphics.endFill();
				
				cachebox.graphics.clear();

				cachebox.graphics.lineStyle(1,0xFF0000,0.75);

				cachebox.graphics.beginFill(0xFF0000,0.1);
				cachebox.graphics.moveTo(boxstart.x, boxstart.y);
				cachebox.graphics.lineTo(boxstart.x+(11*3), boxstart.y+(11*3));
				cachebox.graphics.lineTo(boxstart.x, boxstart.y+(11*6));
				cachebox.graphics.lineTo(boxstart.x-(11*3), boxstart.y+(11*3));
				cachebox.graphics.endFill();
				
				targetcache.bitmapData.draw(cacheline);
				targetcache.bitmapData.draw(cachebox);
			}
			else if (shoot.weapon==0)
			{
			
				var boxstart = IsoMetrics.tiletopix_xy(shoot.location.x, shoot.location.y);
				boxstart.x+=12;
				boxstart.y-=0;

				var cachebox : Shape = new Shape();

				cachebox.graphics.clear();
				cachebox.graphics.lineStyle(1,0x00FF00,0.75);
				cachebox.graphics.beginFill(0x00FF00,0.2);
				cachebox.graphics.moveTo(boxstart.x-(11*1), boxstart.y-(11*5));
				cachebox.graphics.lineTo(boxstart.x+(11*0), boxstart.y-(11*4));
				cachebox.graphics.lineTo(boxstart.x+(11*1), boxstart.y-(11*5));
				cachebox.graphics.lineTo(boxstart.x+(11*6), boxstart.y+(11*0));
				cachebox.graphics.lineTo(boxstart.x+(11*5), boxstart.y+(11*1));
				cachebox.graphics.lineTo(boxstart.x+(11*6), boxstart.y+(11*2));
				cachebox.graphics.lineTo(boxstart.x+(11*1), boxstart.y+(11*7));
				cachebox.graphics.lineTo(boxstart.x+(11*0), boxstart.y+(11*6));
				cachebox.graphics.lineTo(boxstart.x-(11*1), boxstart.y+(11*7));
				cachebox.graphics.lineTo(boxstart.x-(11*6), boxstart.y+(11*2));
				cachebox.graphics.lineTo(boxstart.x-(11*5), boxstart.y+(11*1));
				cachebox.graphics.lineTo(boxstart.x-(11*6), boxstart.y+(11*0));
				cachebox.graphics.endFill();

				targetcache.bitmapData.draw(cachebox);
				
			}
			
		}		
		
		#end
	
	}
	
	static public function draw_currenttarget()
	{
	
		// draw the current target's specific shape and line, and update the tower angle sprite
	
		#if flash9

		var distx : Float = InputHandler.cursx-targetstart.x;
		var disty : Float = InputHandler.cursy-targetstart.y;
		
		var tdist : Float = Math.sqrt(distx*distx + disty*disty);
		
		var dirx : Float = (distx/tdist);
		var diry : Float = (disty/tdist);

		var goodlocation = false;
		
		if (Dist.IntPoint(new IntPoint(HUD.ctx,HUD.cty), new IntPoint(lastx, lasty))<lastshooter.dist && HUD.ctx<25 && HUD.cty<25
																					   && HUD.ctx>0 && HUD.cty>0)
		{
			goodlocation=true;
		}
		
		targetline.graphics.clear();

		if (goodlocation)
		{
			targetline.graphics.lineStyle(1,0xFFFFFF);
			targetline.graphics.beginFill(0xFFFFFF,0.8);
			targetline.graphics.moveTo(targetstart.x+11+(diry*5),targetstart.y+4-(dirx*5));
			targetline.graphics.lineTo(targetstart.x+11-(diry*5),targetstart.y+4+(dirx*5));
			targetline.graphics.lineTo(InputHandler.cursx,InputHandler.cursy);
			targetline.graphics.endFill();
		}		

		var startpoint = IsoMetrics.tiletopix_xy(HUD.ctx,HUD.cty);
		startpoint.x+=12;
		startpoint.y-=22;
		
		targetbox.graphics.clear();

		if (goodlocation)
		{
			targetbox.graphics.lineStyle(1,0xFFFFFF);
			targetbox.graphics.beginFill(0xAAFFAA,0.8);
		}
		else
		{
			targetbox.graphics.lineStyle(1,0x888888);
			targetbox.graphics.beginFill(0x000000,0.8);
		}

		targetbox.graphics.moveTo(startpoint.x, startpoint.y);
		targetbox.graphics.lineTo(startpoint.x+(11*3), startpoint.y+(11*3));
		targetbox.graphics.lineTo(startpoint.x, startpoint.y+(11*6));
		targetbox.graphics.lineTo(startpoint.x-(11*3), startpoint.y+(11*3));
		targetbox.graphics.endFill();
		
		var vecx = targetstart.x + 12 - InputHandler.cursx;
		var vecy = targetstart.y - InputHandler.cursy;
		var startdist = Math.sqrt((vecx*vecx)+(vecy*vecy));
		
		lastshooter.angle = Math.atan2(vecx/startdist, vecy/startdist);
		lastshooter.drawangle();
		
		#end
		
	}

}
